import {URL,VERSION} from "../config/api.js"

import {NO_LOGIN} from "../config/nologin.js"

module.exports = {
  request:(api,method="GET",param={},header={})=>{
      const url = URL + VERSION + api
      return new Promise((reslove,reject)=>{
          // follow 在不在['index','find']
        //验证请求的接口、是否需要用户登录
        let token = wx.getStorageSync('token')// 同步获取缓存中的数据
        let userInfo = wx.getStorageSync('userInfo')
        
        if(NO_LOGIN.indexOf(api) == -1) {
            if(token == '') {
                throw "请先登录"
            }
        }
        header ={Authorization:token,UID:userInfo.id}
        wx.showLoading({
          title: '正在加载...',
        })
        wx.request({
          url,
          method,
          data:param,
          header:header,
          success:res=>{
            wx.hideLoading({
              success: (res) => {},
            })
            console.log(res)
            if(res.data.code == 200) {
              wx.showToast({
                title: '加载成功',
              })
             reslove(res.data)
            }else if (res.data.code == 1001) {
              wx.showToast({
                title: '请先登录',
              })
              reject("请先登录")
            } else {
              wx.showToast({
                title: '请求失败',
                icon:"error"
              })
               reject("请求失败")
            }
          },
          fail:res=>{
            wx.hideLoading({
              success: (res) => {},
            })
            reject("请求失败")
          }
        })

      })
  }
}